<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth;

class UserController extends Controller
{
    //个人设置页面
    public function setting ()
    {
        return view('user.setting');
    }

    //个人设置操作
    public function settingStore ()
    {

    }

    //个人中心
    public function show (User $user)
    {
        //个人信息，关注/粉丝/文章
        $user = User ::withCount([ 'stars', 'fans', 'posts' ]) -> find($user -> id);

        //个人文章列表，区最新的前10条
        $posts = $user -> posts() -> orderBy('created_at', 'desc') -> take(10) -> get();

        //关注的用户，关注用户的关注/粉丝/文章
        $stars = $user -> stars();
        $susers = User ::whereIn('id', $stars -> pluck('star_id')) -> withCount([ 'stars', 'fans', 'posts' ]) -> get();

        //被关注的用户
        $fans = $user -> fans();
        $fusers = User ::whereIn('id', $fans -> pluck('fan_id')) -> withCount([ 'stars', 'fans', 'posts' ]) -> get();
        return view('user/show',compact('user','posts','susers','fusers'));
    }

    //关注用户
    public function fan (User $user)
    {
        $me = Auth::user();
        $me->doFan($user->id);
        return [
            'erroe' => 0,
            'msg' => ""
        ];

    }

    //取消关注
    public function unfan (User $user)
    {
        $me = Auth::user();
        $me->doUnfan($user->id);
        return [
            'erroe' => 0,
            'msg' => ""
        ];
    }
}
